package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppGoodsMapper;
import com.eco.fanliapp.dao.GoodsImageLableMapper;
import com.eco.fanliapp.entity.AppGoods;
import com.eco.fanliapp.entity.GoodsImageLable;
import com.eco.fanliapp.service.IGoodsImageLableService;
import com.eco.fanliapp.service.ITaoBaoService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品图片标签表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-01-09
 */
@Service
public class GoodsImageLableServiceImpl extends ServiceImpl<GoodsImageLableMapper, GoodsImageLable> implements IGoodsImageLableService {

    @Autowired
    private GoodsImageLableMapper goodsImageLableMapper;
    @Autowired
    private AppGoodsMapper appGoodsMapper;
    @Autowired
    private ITaoBaoService taoBaoService;
    @Autowired
    private RedisTemplateService redisTemplateService;
    private static String redisKey = "app_goods_image_lable======";
    @Override
    public ServerResponse insertAndUpdate(GoodsImageLable goodsImageLable) {
        if(goodsImageLable.getLablePictureUrl() == null || goodsImageLable.getSeckillPictureUrl() == null){
            return ServerResponse.createByErrorMessage("标签图片必须成套上传");
        }
        Integer id = goodsImageLable.getId();
        if(id == null){
            //节假日标签判断时间
            if(goodsImageLable.getPictureStatus() == 2){
                if(goodsImageLable.getStartTime() != null && goodsImageLable.getEndTime() != null){
                    long stratTime = goodsImageLable.getStartTime().getTime();
                    long endTime = goodsImageLable.getEndTime().getTime();
                    QueryWrapper qw = new QueryWrapper();
                    qw.eq("deleted",0);
                    qw.eq("picture_status",2);
                    List<GoodsImageLable> goodsImageLablelist = goodsImageLableMapper.selectList(qw);
                    for(GoodsImageLable list:goodsImageLablelist){
                        long stratTime1 = list.getStartTime().getTime();
                        long endTime1 = list.getEndTime().getTime();
                        if((stratTime >= stratTime1 && stratTime <= endTime1) || (endTime >= stratTime1 && endTime <= endTime1)){
                            return ServerResponse.createByErrorMessage("与节假日标签--"+list.getPictureDescription()+"--生效时间冲突");
                        }
                    }
                }
            }else{//普通标签
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("deleted",0);
                queryWrapper.eq("picture_status",1);
                List<GoodsImageLable> list1 = goodsImageLableMapper.selectList(queryWrapper);
                //普通标签只能存在一套
                if(list1 != null && list1.size() > 0){
                    return ServerResponse.createByErrorMessage("普通标签只能存在一套，如需更改请编辑普通标签");
                }
            }
            int i = goodsImageLableMapper.insert(goodsImageLable);
            if(i > 0){
                redisTemplateService.del(redisKey);
                List<AppGoods> appGoodsList = appGoodsMapper.selectRecommendGoodsAll();
                taoBaoService.getRedisRecommendGoodsList(appGoodsList);

                return ServerResponse.createBySuccessMessage("标签增加成功");
            }else {
                return ServerResponse.createByErrorMessage("标签增加失败");
            }
        }else {
            if(goodsImageLable.getPictureStatus() == 2){
                if(goodsImageLable.getStartTime() != null && goodsImageLable.getEndTime() != null){
                    long stratTime = goodsImageLable.getStartTime().getTime();
                    long endTime = goodsImageLable.getEndTime().getTime();
                    QueryWrapper qw = new QueryWrapper();
                    qw.eq("deleted",0);
                    qw.eq("picture_status",2);
                    List<GoodsImageLable> goodsImageLablelist = goodsImageLableMapper.selectList(qw);
                    for(GoodsImageLable list:goodsImageLablelist){
                        if(list.getStartTime() != null && list.getEndTime() != null && !list.getStartTime().equals("") && !list.getEndTime().equals("")){
                            long stratTime1 = list.getStartTime().getTime();
                            long endTime1 = list.getEndTime().getTime();
                            if(!goodsImageLable.getId().equals(list.getId())){
                                if((stratTime >= stratTime1 && stratTime <= endTime1) || (endTime >= stratTime1 && endTime <= endTime1)){
                                    return ServerResponse.createByErrorMessage("与节假日标签--"+list.getPictureDescription()+"--生效时间冲突");
                                }
                            }
                        }
                    }
                }
            }
            int i = goodsImageLableMapper.updateById(goodsImageLable);
            if (i > 0) {
                redisTemplateService.del(redisKey);
                List<AppGoods> appGoodsList = appGoodsMapper.selectRecommendGoodsAll();
                taoBaoService.getRedisRecommendGoodsList(appGoodsList);
                return ServerResponse.createBySuccessMessage("标签修改成功");
            } else {
                return ServerResponse.createByErrorMessage("标签修改失败");
            }
        }
    }


    @Override
    public ServerResponse selectGoodsImageLableList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("deleted",0);
        qw.orderByDesc("create_time");
        List list = goodsImageLableMapper.selectList(qw);
        if(list == null && list.size() <= 0){
            return ServerResponse.createByErrorMessage("暂无数据");
        }
        return ServerResponse.createBySuccess("查询成功",new PageInfo<>(list));
    }

    @Override
    public ServerResponse deleteGoodsImageLable(Integer id) {
        GoodsImageLable goodsImageLable = goodsImageLableMapper.selectById(id);
        if(goodsImageLable.getEnable() == 1){
            return ServerResponse.createByErrorMessage("生效中，无法删除");
        }
        int i = goodsImageLableMapper.deleteGoodsImageLable(id);
        if(i > 0){
            redisTemplateService.del(redisKey);
            List<AppGoods> appGoodsList = appGoodsMapper.selectRecommendGoodsAll();
            taoBaoService.getRedisRecommendGoodsList(appGoodsList);
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
//    @Override
//    public Map<String, Object> selectGoodsImageLable() {
//        Map<String, Object> lableMap = new HashMap<>();
//        GoodsImageLable goodsImageLable = goodsImageLableMapper.selectGoodsImageLableCommon();//普通标签
//        if(goodsImageLable != null){
//            lableMap.put("commonLable",goodsImageLable.getLablePictureUrl());
//            lableMap.put("commonSeckill",goodsImageLable.getSeckillPictureUrl());
//        }
//        GoodsImageLable goodsImageLable1 = goodsImageLableMapper.selectGoodsImageLable();//节日标签
//        if(goodsImageLable1 != null){
//            lableMap.put("festivalLable",goodsImageLable1.getLablePictureUrl());
//            lableMap.put("festivalSeckill",goodsImageLable1.getSeckillPictureUrl());
//        }
//        if (lableMap == null || lableMap.size() <= 0) {
//            return null;
//        } else {
//            return lableMap;
//        }
//    }
    @Override
    public Map<String,Object> selectGoodsImageLable() {
        //普通标签
        Map<String,Object> lableMap = new HashMap<>();
        if(redisTemplateService.hasKey(redisKey)){
            lableMap = redisTemplateService.getMap(redisKey, Map.class);
        }
        if (lableMap.size() <= 0) {
            GoodsImageLable goodsImageLable = goodsImageLableMapper.selectGoodsImageLableCommon();//普通标签
            if(goodsImageLable != null){
                lableMap.put("commonLable",goodsImageLable.getLablePictureUrl());
                lableMap.put("commonSeckill",goodsImageLable.getSeckillPictureUrl());
            }
            GoodsImageLable goodsImageLable1 = goodsImageLableMapper.selectGoodsImageLable();//节日标签
            if(goodsImageLable1 != null){
                lableMap.put("festivalLable",goodsImageLable1.getLablePictureUrl());
                lableMap.put("festivalSeckill",goodsImageLable1.getSeckillPictureUrl());
            }
            if(lableMap.size() > 0){
                redisTemplateService.setMap(redisKey,lableMap,0l);
                redisTemplateService.expire(redisKey,60);
            }
        }
        if (lableMap == null || lableMap.size() <= 0) {
            return null;
        } else {
            return lableMap;
        }
    }

    public static void main(String[] args) {
        Date date = new Date();
        long time = date.getTime();
        System.out.println(time);
    }
}
